#!/usr/bin/env rc

fn exists {
  ! ~ $#* 0
}

fn has_color {
  exists $$1 && exists $^(BOLD_$1)
}

fn has_theme {
  has_color BLACK && \
    has_color RED && \
    has_color GREEN && \
    has_color YELLOW && \
    has_color BLUE && \
    has_color MAGENTA && \
    has_color CYAN && \
    has_color WHITE
}

fn esc {
  printf '\e]%s%s' $1 $2
}

if (has_theme) {
  esc P0 $BLACK
  esc P1 $RED
  esc P2 $GREEN
  esc P3 $YELLOW
  esc P4 $BLUE
  esc P5 $MAGENTA
  esc P6 $CYAN
  esc P7 $WHITE

  esc P8 $BOLD_BLACK
  esc P9 $BOLD_RED
  esc PA $BOLD_GREEN
  esc PB $BOLD_YELLOW
  esc PC $BOLD_BLUE
  esc PD $BOLD_MAGENTA
  esc PE $BOLD_CYAN
  esc PF $BOLD_WHITE

  if (exists $FOREGROUND) {
    printf '\e]10;#%s\a' $FOREGROUND
  }

  if (exists $BACKGROUND) {
    printf '\e]11;#%s\a' $BACKGROUND
  }

  if (exists $CURSOR_COLOR) {
    printf '\e]12;#%s\a' $CURSOR_COLOR
  }
} else {
  echo 'No theme!'
}
